package org.zjx.indetify;

import lombok.SneakyThrows;
import org.zjx.entity.Address;
import org.zjx.entity.Font;
import org.zjx.entity.ParagraphBean;
import org.zjx.indetify.util.FontUtil;
import org.zjx.resource.GameRes;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IdentifyFontManager {
    private static final String identifyResultTag = "identifyResult";

    public static List<Font> getFonts(String attributeName) {
        return GameRes.getInstance().loadListResource(GameRes.RES_FONT, null,attributeName, Font.class);
    }

    public static void saveFonts(String attributeName, List<Font> fonts) {
        GameRes.getInstance().saveResource(fonts, GameRes.RES_FONT, null, attributeName);
    }

    @SneakyThrows
    public static List<ParagraphBean.Word> identify(BufferedImage image, List<Color> colors, List<Font> fonts, int fontHeight) {
        ParagraphBean paragraphBean = new ParagraphBean();
        paragraphBean.add(FontUtil.categories(identifyResultTag, colors, image, fonts, fontHeight,false));
        if (paragraphBean.getChildren() == null || paragraphBean.getChildren().isEmpty()) return null;
        List<ParagraphBean.Word> words =  paragraphBean.getChildren().get(0).getWords();
        if (words == null || words.isEmpty()) return null;
        boolean record = false;
        for (ParagraphBean.Word word : words) {
            if (word.getName().contains("null")){
                if (!record){
                    IdentifyErrorManager.saveIdentifyFontErrorImage(image);
                    record = true;
                }
                word.setName(word.getName().replace("null",""));
                break;
            }
        }
        return words;
    }

    @SneakyThrows
    public static List<ParagraphBean.Word> identifyReverse(BufferedImage image, List<Color> colors, List<Font> fonts, int fontHeight) {
        ParagraphBean paragraphBean = new ParagraphBean();
        paragraphBean.add(FontUtil.categories(identifyResultTag, colors, image, fonts, fontHeight,true));
        if (paragraphBean.getChildren() == null || paragraphBean.getChildren().isEmpty()) return null;
        List<ParagraphBean.Word> words =  paragraphBean.getChildren().get(0).getWords();
        if (words == null || words.isEmpty()) return null;
        boolean record = false;
        for (ParagraphBean.Word word : words) {
            if (word.getName().contains("null")){
                if (!record){
                    IdentifyErrorManager.saveIdentifyFontErrorImage(image);
                    record = true;
                }
                word.setName(word.getName().replace("null",""));
                break;
            }
        }
        return words;
    }

    public static String identifyString(BufferedImage image, List<Color> colors, List<Font> fonts, int fontHeight) {
        List<ParagraphBean.Word> words = identify(image, colors, fonts, fontHeight);
        if (words == null || words.isEmpty()) return null;
        StringBuilder stringBuilder = new StringBuilder();
        for (ParagraphBean.Word word : words) {
            if (word.getName().contains("null")) word.setName(word.getName().replace("null",""));
            stringBuilder.append(word.getName());
        }
        return stringBuilder.toString().replace("，，","”");
    }

    public static String identifyStringReverse(BufferedImage image, List<Color> colors, List<Font> fonts, int fontHeight) {
        List<ParagraphBean.Word> words = identifyReverse(image, colors, fonts, fontHeight);
        if (words == null || words.isEmpty()) return null;
        StringBuilder stringBuilder = new StringBuilder();
        for (ParagraphBean.Word word : words) {
            if (word.getName().contains("null")) word.setName(word.getName().replace("null",""));
            stringBuilder.append(word.getName());
        }
        return stringBuilder.toString().replace("，，","”");
    }

    public static Address matcherLocation(String location) {
        // 正则表达式：提取城市名称和坐标
        String regex = "([\\w\\u4e00-\\u9fa5]+)【([0-9]+)，([0-9]+)】";
        // 创建 Pattern 对象
        Pattern pattern = Pattern.compile(regex);

        // 创建 matcher 对象
        Matcher matcher = pattern.matcher(location);
        if (matcher.find()) {
            // 提取城市名称和坐标
            String city = matcher.group(1);  // 城市名称
            int x = Integer.parseInt(matcher.group(2));  // X 坐标
            int y = Integer.parseInt(matcher.group(3));  // Y 坐标

            // 创建 Address 对象
            return new Address(city, x, y);
        } else
            return null;
    }


    public static Point matcherMap(String location) {
        String regex = "([0-9]+)，([0-9]+)";
        // 创建 Pattern 对象
        Pattern pattern = Pattern.compile(regex);

        // 创建 matcher 对象
        Matcher matcher = pattern.matcher(location);
        if (matcher.find()) {
            // 提取城市名称和坐标
            int x = Integer.parseInt(matcher.group(1));  // X 坐标
            int y = Integer.parseInt(matcher.group(2));  // Y 坐标

            // 创建 Address 对象
            return new Point(x, y);
        } else
            return null;
    }

    //提起合成旗次数
    public static Integer extractFlagsNumber(String input){
//        String input = "场景：长安城，还能使用50次 ";
        // 正则表达式匹配场景和次数（兼容前后空格）
        String regex = "场景：([^，]+)，还能使用\\s*(\\d+)\\s*次";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        if (matcher.find()) {
            String scene = matcher.group(1).trim(); // 提取场景并去除首尾空格
            int usageCount = Integer.parseInt(matcher.group(2)); // 提取次数

            System.out.println("提取结果：");
            System.out.println("场景名称: " + scene);
            System.out.println("剩余次数: " + usageCount);
            return usageCount;
        } else {
            System.err.println("输入格式不符合要求！");
            return null;
        }
    }

    //提起合成旗城市
    public static String extractFlagsCity(String input){
//        String input = "场景：长安城，还能使用50次 ";
        // 正则表达式匹配场景和次数（兼容前后空格）
        String regex = "场景：([^，]+)，还能使用\\s*(\\d+)\\s*次";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        if (matcher.find()) {
            String scene = matcher.group(1).trim(); // 提取场景并去除首尾空格
            int usageCount = Integer.parseInt(matcher.group(2)); // 提取次数

            System.out.println("提取结果：");
            System.out.println("场景名称: " + scene);
            System.out.println("剩余次数: " + usageCount);
            return scene;
        } else {
            System.err.println("输入格式不符合要求！");
            return null;
        }
    }
}
